package com.example.demo.service.serviceImpl;

import com.example.demo.enums.OcrEnum;
import com.example.demo.service.Tencent_OCRService;
import com.example.demo.sign.TencentAISign;
import com.example.demo.sign.TencentAISignSort;
import com.example.demo.utils.Base64Util;
import com.example.demo.utils.HttpsUtil4Tencent;
import com.example.demo.utils.TencentAPI;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
@Service
@Scope("prototype")
public class Tencent_OCRServiceImpl implements Tencent_OCRService{
    @Override
    public String getOCR_(byte[] file, String ocr) throws Exception {
        //时间戳
        String time_stamp = System.currentTimeMillis()/1000+"";
        String img64 = Base64Util.encode(file);
        //随机字符串
        String nonce_str = TencentAISign.getRandomString(10);
        Map<String,String> body = new HashMap<>();   //【接口属性】（注意修改成自己要的参数）
        body.put("app_id", String.valueOf(TencentAPI.APP_ID_AI));
        body.put("time_stamp",time_stamp);
        body.put("nonce_str", nonce_str);
        body.put("image", img64);
        if(ocr.equals(String.valueOf(OcrEnum.Id.getCode()))){
            body.put("card_type", "0");
        }
        String sign = TencentAISignSort.getSignature(body);
        body.put("sign", sign);
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/x-www-form-urlencoded");
        HttpResponse responseBD;
        if(ocr.equals(String.valueOf(OcrEnum.Id.getCode()))){
            responseBD = HttpsUtil4Tencent.doPostTencentAI(TencentAPI.OCR_Id, headers, body);
        }
        else if(ocr.equals(String.valueOf(OcrEnum.BuinessCard.getCode()))){
           responseBD = HttpsUtil4Tencent.doPostTencentAI(TencentAPI.OCR_BUINESSCARD, headers, body);
        }
        else if(ocr.equals(String.valueOf(OcrEnum.BuinessLicense.getCode()))){
           responseBD = HttpsUtil4Tencent.doPostTencentAI(TencentAPI.OCR_BUINLICENSE, headers, body);
        }
        else if(ocr.equals(String.valueOf(OcrEnum.BankCard.getCode()))){
            responseBD = HttpsUtil4Tencent.doPostTencentAI(TencentAPI.OCR_BANK, headers, body);
        }
        else{    //都没匹配直接配置通用
            responseBD = HttpsUtil4Tencent.doPostTencentAI(TencentAPI.OCR_GENERAL, headers, body);
        }
        String json = EntityUtils.toString(responseBD.getEntity());
        return json;
    }

    @Override
    public String getOCR_DriverLicense(byte[] file, String type) throws Exception {
        //时间戳
        String time_stamp = System.currentTimeMillis()/1000+"";
        String img64 = Base64Util.encode(file);
        //随机字符串
        String nonce_str = TencentAISign.getRandomString(10);
        Map<String,String> body = new HashMap<>();   //【接口属性】（注意修改成自己要的参数）
        body.put("app_id", String.valueOf(TencentAPI.APP_ID_AI));
        body.put("time_stamp",time_stamp);
        body.put("nonce_str", nonce_str);
        body.put("image", img64);
        body.put("type", type);
        String sign = TencentAISignSort.getSignature(body);
        body.put("sign", sign);
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/x-www-form-urlencoded");
        HttpResponse responseBD = HttpsUtil4Tencent.doPostTencentAI(TencentAPI.OCR_DRIVER, headers, body);
        String json = EntityUtils.toString(responseBD.getEntity());
        return json;
    }
}
